package com.bifang.module.file.core.biz.impl;

import cn.hutool.core.convert.Convert;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.cache.jetcache.utils.JetCacheUtils;
import com.bifang.module.file.common.cache.FileCacheConst;
import com.bifang.module.file.common.vo.FileEnvVO;
import com.bifang.module.file.core.biz.FileEnvIntegration;
import com.bifang.module.file.dao.model.FileEnv;
import com.bifang.module.file.dao.service.FileEnvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Duration;

@Service
public class FileEnvIntegrationImpl implements FileEnvIntegration {
    @Autowired FileEnvService fileEnvService;
    @Override
    public FileEnvVO getFileEnvDetail(Long fileEnvId) {
        if (fileEnvId == null) {
            throw new AppException("文件环境id不能为空");
        }
        FileEnvVO fileEnvVO;
        String key = fileEnvId.toString();
        // 判断是否存在缓存中 在缓存中的，获取缓存数据
        Cache cache = JetCacheUtils.create(FileCacheConst.FILEENVINFO, CacheType.BOTH, Duration.ofSeconds(86400),Duration.ofSeconds(300));
        if (cache.get(key)!=null) {
            fileEnvVO = (FileEnvVO) cache.get(key);
        } else {
            FileEnv fileEnv = fileEnvService.getById(fileEnvId);
            if (fileEnv == null) {
                throw new AppException("文件服务的环境未配置");
            }
            if (BoolEnum.TRUE.getType().equals(fileEnv.getDel())) {
                throw new AppException("文件服务的环境已删除");
            }
            fileEnvVO = Convert.convert(FileEnvVO.class, fileEnv);
            cache.put(key,fileEnvVO);
        }
        return fileEnvVO;
    }
}
